package com.tcdp.daos;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.tcdp.models.BlogCategory;

public class BlogCategoryDao extends AbstractDao {

	public BlogCategory findById(int id){
		BlogCategory blogCategory = em.find(BlogCategory.class, id);
		return blogCategory;
	}
	
	public BlogCategory findByUrl(String url){
		BlogCategory blogCategory = null;
		
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<BlogCategory> cq = cb.createQuery(BlogCategory.class);
		Root<BlogCategory> root = cq.from(BlogCategory.class);
		cq.select(root).where(cb.equal(root.<String>get("blogCategoryUrl"), url));
		TypedQuery<BlogCategory> tq = em.createQuery(cq);
		try{
			blogCategory = tq.getSingleResult();
		}catch(NoResultException e){
			throw404(e);
		}
		
		return blogCategory;
	}
	
}
